﻿namespace WMS6.Web.Inbound
{
    using System;
    using System.Data;
    using System.Drawing;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.InboundDetailServiceRef;
    using WMS6.Web.UserControls;

    public class ReceiptInquiryList : BasePage
    {
        protected Button btnAction;
        protected Button btnDelete;
        protected Button btnEdit;
        protected Button btnExport;
        protected Button btnList;
        protected Button btnNew;
        protected Button btnRefresh;
        protected Button btnReport;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F4042;
        protected GridView gvReceipt;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlHead Head1;
        protected Label lblMsg;
        protected Label lblReceiptInquiryList;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected UpdatePanel up1;

        private void BindData2ReceiptList(ReceiptDetailQueryEntity receiptQuery)
        {
            this.PagerFooter.CurrentPage = int.Parse(receiptQuery.CurrentPage);
            DataSet ds = new DataSet();
            ds = InboundDetailServiceClientProxy.GetInstance().GetReceiptInquiryInfoByQueryEntity(receiptQuery);
            base.BindData2GridView(this.gvReceipt, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            ReceiptDetailQueryEntity receiptQuery = this.GetReceiptQueryEntity();
            receiptQuery.IsGetAll = true;
            this.hdnActionID.Value = "EXPORT";
            string currentPageIndex = receiptQuery.CurrentPage;
            receiptQuery.CurrentPage = "0";
            this.gvReceipt.Columns[1].Visible = false;
            this.gvReceipt.AllowSorting = false;
            this.BindData2ReceiptList(receiptQuery);
            this.hdnActionID.Value = "";
            this.gvReceipt.ShowFooter = false;
            receiptQuery.IsGetAll = false;
            receiptQuery.CurrentPage = currentPageIndex;
            this.SetReceiptQueryEntity(receiptQuery);
            this.hdnActionID.Value = "";
            base.Export2Excel("ReceiptInquiry.xls", this.gvReceipt);
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            ReceiptDetailQueryEntity receiptQuery = this.GetReceiptQueryEntity();
            receiptQuery.CurrentPage = "0";
            this.BindData2ReceiptList(receiptQuery);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            ReceiptDetailQueryEntity receiptQuery = this.GetReceiptQueryEntity();
            receiptQuery.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2ReceiptList(receiptQuery);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            ReceiptDetailQueryEntity receiptQuery = this.GetReceiptQueryEntity();
            receiptQuery.ReceiptID = "";
            receiptQuery.PoID = "";
            receiptQuery.ExternalReceiptID = "";
            receiptQuery.OwnerID = "";
            receiptQuery.Status = "";
            receiptQuery.ExpectedReceiptDate = "";
            receiptQuery.ReceiptType = "";
            receiptQuery.LottableMatchRequired = "";
            receiptQuery.SkuClassID = "";
            receiptQuery.ReceiptDate = "";
            receiptQuery.ExternalLot = "";
            receiptQuery.VendorID = "";
            this.BindData2ReceiptList(receiptQuery);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            ReceiptDetailQueryEntity receiptQuery = this.GetReceiptQueryEntity();
            if (int.Parse(receiptQuery.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                receiptQuery.CurrentPage = (int.Parse(receiptQuery.CurrentPage) + 1).ToString();
                this.BindData2ReceiptList(receiptQuery);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            ReceiptDetailQueryEntity receiptQuery = this.GetReceiptQueryEntity();
            if (int.Parse(receiptQuery.CurrentPage) > 0)
            {
                receiptQuery.CurrentPage = (int.Parse(receiptQuery.CurrentPage) - 1).ToString();
                this.BindData2ReceiptList(receiptQuery);
            }
        }

        private ReceiptDetailQueryEntity GetReceiptQueryEntity()
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                return (base.PageQueryEntityViewState as ReceiptDetailQueryEntity);
            }
            return (base.PageQueryEntitySession as ReceiptDetailQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                ReceiptDetailQueryEntity receiptQuery = this.GetReceiptQueryEntity();
                receiptQuery.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2ReceiptList(receiptQuery);
            }
        }

        protected void gvReceipt_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F4042", this.gvReceipt);
        }

        protected void gvReceipt_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                if (!string.IsNullOrEmpty(this.hdnActionID.Value))
                {
                    ((HyperLink) e.Row.FindControl("HyperLink1")).Enabled = false;
                }
                if (this.hdnActionID.Value != "EXPORT")
                {
                    base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblSkuDescr"));
                }
                string diffQty = DataBinder.Eval(e.Row.DataItem, "DIFF_QTY").ToString();
                if (!(string.IsNullOrEmpty(diffQty) || !(decimal.Parse(diffQty) != 0M)))
                {
                    ((Label) e.Row.FindControl("lblDiffQty")).BackColor = Color.Yellow;
                }
            }
        }

        protected void gvReceipt_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                ReceiptDetailQueryEntity receiptQuery = this.GetReceiptQueryEntity();
                receiptQuery.SortField = e.SortExpression;
                if (receiptQuery.SortDirection == "ASC")
                {
                    receiptQuery.SortDirection = "DESC";
                }
                else if (receiptQuery.SortDirection == "DESC")
                {
                    receiptQuery.SortDirection = "ASC";
                }
                this.BindData2ReceiptList(receiptQuery);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            ReceiptDetailQueryEntity receiptQuery = new ReceiptDetailQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    receiptQuery.ReceiptID = this.PreviousPage.ReceiptID;
                    receiptQuery.PoID = this.PreviousPage.PoID;
                    receiptQuery.ExternalReceiptID = this.PreviousPage.ExternReceiptID;
                    receiptQuery.OwnerID = this.PreviousPage.OwnerID;
                    receiptQuery.IsActive = this.PreviousPage.IsActive;
                    receiptQuery.ExpectedReceiptDate = this.PreviousPage.ExpectedReceiptData;
                    receiptQuery.ReceiptType = this.PreviousPage.ReceiptType;
                    receiptQuery.SkuID = this.PreviousPage.Sku;
                    receiptQuery.SkuClassID = this.PreviousPage.SkuClassID;
                    receiptQuery.ReceiptDate = this.PreviousPage.ReceiptDate;
                    receiptQuery.ExternalLot = this.PreviousPage.ExternalLot;
                    receiptQuery.VendorID = this.PreviousPage.VendorID;
                }
                else
                {
                    receiptQuery.ReceiptID = "";
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            receiptQuery.IsGetAll = false;
            receiptQuery.CurrentPage = "0";
            receiptQuery.PageSize = this.gvReceipt.PageSize.ToString();
            receiptQuery.SortField = "RECEIPT_ID DESC,RECEIPT_LINE_ID";
            receiptQuery.SortDirection = "ASC";
            this.PagerFooter.PageSize = this.gvReceipt.PageSize;
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["export"];
            }
            this.SetReceiptQueryEntity(receiptQuery);
            this.BindData2ReceiptList(receiptQuery);
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.btnExport_Click(null, null);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetReceiptQueryEntity(ReceiptDetailQueryEntity receiptQuery)
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                base.PageQueryEntityViewState = receiptQuery;
            }
            else
            {
                base.PageQueryEntitySession = receiptQuery;
            }
        }

        public ReceiptInquiryQuery PreviousPage
        {
            get
            {
                return (ReceiptInquiryQuery) base.PreviousPage;
            }
        }
    }
}

